#tag Class
Protected Class DecissionAI2
Implements ArtificialPlayer
	#tag Method, Flags = &h0
		Sub Calculate()
		  // Part of the ArtificialPlayer interface.
		  
		  dim theTile as board.Tile
		  dim possibleCoordinates() as Coordinates
		  dim onTop as String //side that is on top (north, east, south, west)
		  dim evaluated() as EvaluatorAI2
		  dim i as integer
		  
		  
		  theTile = game.GetNextTile
		  onTop = "north"
		  possibleCoordinates = game.GetPossibleCoordinatesForTile
		  
		  if theTile.monastry = true then
		    for i = 0 to ubound(possibleCoordinates)
		      dim ev as New EvaluatorAI2
		      ev.AddScore 50
		      ev.pos = "n"
		      ev.xy = possibleCoordinates(i)
		    next
		  else
		    if theTile.IsSingleTown = true then
		      
		      
		      
		    end if
		  end if
		  
		  
		  
		  
		  
		  //evalutate
		  for i = 0 to ubound(evaluated)
		    if evaluated(i).GetScore >= 50 then //monastery, occupy at any cost
		      AddTile(evaluated(i),theTile)
		      Occupy ("Monastery", theTile)
		      exit for
		    end if
		  next
		  
		  player.EndRound
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Constructor(p as game . Player)
		  self.player = p
		  self.game = p.GetGame
		  self.board = game.GetBoard
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h21
		Private Sub AddTile(ev as EvaluatorAI2, theTile as Tile)
		  theTile.ResetPosition // just in case
		  
		  select case ev.pos
		  case "e"
		    theTile.RotateClockwise
		  case "s"
		    theTile.RotateClockwise
		    theTile.RotateClockwise
		  case "w"
		    theTile.RotateCounterClockwise
		  end select
		  
		  if player.AddTileAtPosition(theTile, ev.xy) = false then
		    app.RaiseError(501, "Placement of Tile was not possible", "EvaluatorAI2.AddTile", 3)
		  end if
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h21
		Private Sub Occupy(slotType as string, theTile As Tile)
		  dim possibleSlots() as board.slot
		  dim i as integer
		  possibleSlots = theTile.grid.GetPossibleSlots
		  
		  //if slots are existing - take the first one
		  if ubound(possibleSlots) > -1 then
		    for i = 0 to ubound(possibleSlots)
		      if possibleSlots(i).GetType = slotType then
		        player.Occupy possibleSlots(i).GetPosition
		        'dbg = dbg + " occupy " + possibleSlots(i).GetType
		        exit for
		      end if
		    next
		  else
		    app.RaiseError (502, "Tile without possible Slots", "DecissionAI2.Occupy", 3)
		  end if
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h21
		Private Function GetMyTowns() As Tile()
		  dim tiles() as Tile
		  dim towns() as Tile
		  
		  tiles = board.GetTilesByOwner(player)
		  
		  for each t as Tile in tiles
		    if t.grid.GetOccupiedType = "Town" then
		      towns.Append t
		    end if
		  next
		  
		  return towns
		End Function
	#tag EndMethod


	#tag Property, Flags = &h21
		Private player As game.Player
	#tag EndProperty

	#tag Property, Flags = &h21
		Private game As game.ConcreteGame
	#tag EndProperty

	#tag Property, Flags = &h21
		Private board As board.Gameboard
	#tag EndProperty


	#tag ViewBehavior
		#tag ViewProperty
			Name="Name"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Index"
			Visible=true
			Group="ID"
			InitialValue="-2147483648"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Super"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Left"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Top"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
	#tag EndViewBehavior
End Class
#tag EndClass
